package com.idealighter.game.games.fruitslot.manager;

import com.google.inject.Inject;
import com.google.inject.Singleton;

import com.idealighter.game.core.common.Game;
import com.idealighter.game.core.constant.room.RoomActiveConstant;
import com.idealighter.game.dictionary.dic.FruitslotRoomDic;
import com.idealighter.game.dictionary.dic.GamesDic;
import com.idealighter.game.dictionary.domain.FruitslotRoomDomain;
import com.idealighter.game.dictionary.domain.GamesDomain;
import com.idealighter.game.games.common.InstanceState;
import com.idealighter.game.games.fruitslot.struct.FruitslotBaseRoom;
import com.idealighter.game.games.fruitslot.struct.FruitslotRoom;
import com.idealighter.game.games.fruitslot.struct.FruitslotSeat;
import com.idealighter.game.games.fruitslot.struct.FruitslotTable;
import com.idealighter.utils.check.EmptyUtil;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 水浒传数据管理 .
 * 
 * @date 2015年9月28日 下午2:13:25
 *
 */
@Singleton
public class FruitslotDataMgr {

  private static final Logger LOG = LoggerFactory.getLogger(FruitslotDataMgr.class);

  private final Map<Long, FruitslotSeat> playerSeats = new HashMap<>();
  // 水浒游戏房间
  private final Map<Integer, FruitslotBaseRoom> roomMap = new HashMap<>();

  private FruitslotRoomDic roomDic;

  private final GamesDic gamesDic;

  /**
   * 构造函数.
   * 
   * @param roomDic 房间dic.
   * @param gamesDic 游戏dic.
   */
  @Inject
  public FruitslotDataMgr(FruitslotRoomDic roomDic, GamesDic gamesDic) {
    this.roomDic = roomDic;
    this.gamesDic = gamesDic;

    createRooms();
  }

  /**
   * 创建房间 .
   *
   * @author abin
   * @date 2018年4月28日 下午2:10:42
   * @param roomDomain 房间dic信息.
   */
  private void createRoom(FruitslotRoomDomain roomDomain) {
    if (roomDomain != null && roomDomain.getIsActive() == RoomActiveConstant.ACTIVE) {
      GamesDomain gamesDom = gamesDic.get(Game.FRUIT_SLOT.getType());
      if (gamesDom != null && gamesDom.getActive()) {
        this.roomMap.put(roomDomain.getId(), new FruitslotBaseRoom(roomDomain));
        LOG.info("[水果拉霸]创建房间[{}][{}]", roomDomain.getId(), roomDomain.getName());
      }
    }
  }

  /**
   * 创建房间.
   * 
   * @return 房间map.
   */
  private void createRooms() {
    List<FruitslotRoomDomain> roomDomains = roomDic.list();
    if (EmptyUtil.listIsNotEmpty(roomDomains)) {
      for (FruitslotRoomDomain roomDomain : roomDomains) {
        createRoom(roomDomain);
      }
    }
  }

  /**
   * 重新加载房间，并返回最新房间id列表.
   *
   * @author abin
   * @date 2018年5月16日 下午2:48:38
   * @return 房间id列表.
   */
  public List<Integer> reloadRoom() {
    roomDic.load();
    List<Integer> ids = new ArrayList<>();
    GamesDomain gamesDom = gamesDic.get(Game.FRUIT_SLOT.getType());
    if (gamesDom != null && gamesDom.getActive()) {
      List<FruitslotRoomDomain> rooms = roomDic.list();
      if (EmptyUtil.listIsNotEmpty(rooms)) {
        for (FruitslotRoomDomain roomDomain : rooms) {
          if (roomDomain != null && roomDomain.getIsActive() == RoomActiveConstant.ACTIVE) {
            ids.add(roomDomain.getId());
          }
        }
      }
    }
    return ids;
  }

  /**
   * 开启房间.
   *
   * @author abin
   * @date 2018年4月28日 下午2:14:26
   * @param roomId 房间id.
   * @return return old room(delete) list.
   */
  public List<FruitslotRoom> startRoom(int roomId, boolean reload) {
    if (reload) {
      roomDic.load();
    }

    List<FruitslotRoom> deleteRooms = new ArrayList<>();

    GamesDomain gamesDom = gamesDic.get(Game.FRUIT_SLOT.getType());
    if (gamesDom != null && gamesDom.getActive()) {
      FruitslotRoomDomain roomDomain = roomDic.get(roomId);
      if (roomDomain != null && roomDomain.getIsActive() == RoomActiveConstant.ACTIVE) {
        FruitslotBaseRoom baseRoom = roomMap.get(roomId);

        if (baseRoom == null) {
          createRoom(roomDomain);
        } else {
          deleteRooms = baseRoom.renewRoom(roomDomain);
        }
      }
    }
    return deleteRooms;
  }

  /**
   * 获取最新房间实例.
   * 
   * @param roomId 房间id.
   * @return 21点房间.
   */
  public FruitslotRoom getNewestRoom(int roomId) {
    FruitslotRoom room = null;
    FruitslotBaseRoom baseRoom = roomMap.get(roomId);
    if (baseRoom != null) {
      room = baseRoom.getNewInstance();
    }
    return room;
  }


  /**
   * 获取所有实例 .
   *
   * @author abin
   * @date 2018年4月28日 下午4:32:06
   * @param roomId 房间id.
   * @return 获取所有实例.
   */
  public List<FruitslotRoom> deleteRoom(int roomId) {
    roomDic.load();
    List<FruitslotRoom> rooms = null;
    FruitslotBaseRoom baseRoom = roomMap.get(roomId);
    if (baseRoom != null) {
      rooms = new ArrayList<>(baseRoom.deleteAllInstance());
    }
    return rooms;
  }

  /**
   * 实例id获取房间实例 .
   *
   * @author abin
   * @date 2018年4月27日 下午6:01:41
   * @param instanceId 实例id.
   * @return 21点房间.
   */
  public FruitslotRoom getRoomByIntanceId(int instanceId) {
    FruitslotRoom room = null;
    int roomId = getRoomIdByIntanceId(instanceId);
    FruitslotBaseRoom baseRoom = roomMap.get(roomId);
    if (baseRoom != null) {
      room = baseRoom.getByInstanceId(instanceId);
    }
    return room;
  }


  /**
   * 校验是否remove room.
   *
   * @author abin
   * @date 2018年4月28日 下午7:39:33
   * @param baseRoom 基本房间.
   */
  public void checkRemoveRoom(FruitslotBaseRoom baseRoom) {
    if (baseRoom.allInstance() == null || baseRoom.allInstance().isEmpty()) {
      roomMap.remove(baseRoom.getRoomId());
      baseRoom = null;
    }

  }

  /**
   * 清空房间实例.
   *
   * @author abin
   * @date 2018年4月28日 下午5:55:32
   * @param roomId 房间id.
   * @param instanceId 实例id.
   */
  public boolean checkRemoveInstance(int roomId, int instanceId) {
    boolean remove = false;
    FruitslotBaseRoom baseRoom = roomMap.get(roomId);
    if (baseRoom != null) {
      FruitslotRoom room = baseRoom.getByInstanceId(instanceId);

      Collection<FruitslotTable> tables = room.tables();

      if (tables == null || tables.isEmpty()) {
        baseRoom.removeInstance(instanceId);
        checkRemoveRoom(baseRoom);
        remove = true;
      }
    }
    return remove;
  }

  /**
   * 获取所有最新的房间.
   * 
   * @return 21点房间列表.
   */
  public Collection<FruitslotRoom> newestRooms() {
    List<FruitslotRoom> tempRooms = new ArrayList<>();
    for (FruitslotBaseRoom baseRoom : roomMap.values()) {
      FruitslotRoom fruitslotRoom = baseRoom.getNewInstance();
      if (fruitslotRoom != null && fruitslotRoom.getInstanceState().equals(InstanceState.NORMAL)) {
        tempRooms.add(fruitslotRoom);
      }
    }
    return tempRooms;
  }

  /**
   * 获取所有的房间列表 .
   *
   * @author abin
   * @date 2018年4月27日 下午4:22:40
   * @return 所有的房间列表.
   */
  public Collection<FruitslotRoom> allRooms() {
    List<FruitslotRoom> tempRooms = new ArrayList<>();
    for (FruitslotBaseRoom baseRoom : roomMap.values()) {
      Collection<FruitslotRoom> allInstances = baseRoom.allInstance();
      if (allInstances != null && !allInstances.isEmpty()) {
        tempRooms.addAll(allInstances);
      }
    }
    return tempRooms;
  }

  public Collection<FruitslotBaseRoom> allBaseRoom() {
    return roomMap.values();
  }



  /**
   * 获取牌桌.
   * 
   * @param tableId 桌子id.
   * @return 21点桌子.
   */
  public FruitslotTable getTable(int tableId) {
    FruitslotTable table = null;
    int roomId = getRoomIdByTableId(tableId);
    FruitslotBaseRoom baseRoom = roomMap.get(roomId);
    if (baseRoom != null) {
      int instanceId = getInstanceIdByTableId(tableId);
      FruitslotRoom room = baseRoom.getByInstanceId(instanceId);
      if (room != null) {
        table = room.getTables().get(tableId);
      }
    }
    return table;
  }

  /**
   * 根据桌子id计算房间实例id.
   * 
   * @param tableId 桌子Id.
   * @return 房间Id.
   */
  public int getInstanceIdByTableId(int tableId) {
    return tableId / FruitslotTable.MAX_TABLE;
  }

  /**
   * 根据实例id计算房间id .
   *
   * @author abin
   * @date 2018年4月27日 下午5:54:05
   * @param instanceId 实例id.
   * @return 房间id.
   */
  public int getRoomIdByIntanceId(int instanceId) {
    return instanceId / FruitslotRoom.MAX_INSTANCE;
  }

  /**
   * 根据桌子获取房间id .
   *
   * @author abin
   * @date 2018年4月27日 下午5:56:03
   * @param tableId 桌子id.
   * @return 房间id.
   */
  public int getRoomIdByTableId(int tableId) {
    return getRoomIdByIntanceId(getInstanceIdByTableId(tableId));
  }

  /**
   * 获取座位 .
   * 
   * @param playerId 玩家id.
   * @return 座位.
   */
  public FruitslotSeat getPlayerSeat(long playerId) {

    return playerSeats.get(playerId);
  }

  /**
   * 更新玩家座位 .
   * 
   * @param playerId 玩家id.
   * @param seat 座位.
   */
  public void updatePlayerSeats(long playerId, FruitslotSeat seat) {
    playerSeats.put(playerId, seat);
  }

  /**
   * 移除玩家seat.
   * 
   * @param playerId 玩家id.
   */
  public void removePlayerSeat(long playerId) {
    playerSeats.remove(playerId);
  }


}
